'use strict';
/**
 * @name 需求申请的controller
 * @author yud
 */
angular.module('metalabPartnerApp')
    .controller('DemandApplyCtrl', ['$rootScope', '$scope', '$state', 'MetaConstants', 'MetaResources', 'MetaRemind'
        , function ($rootScope, $scope, $state, MetaConstants, MetaResources, MetaRemind) {

            //初始化信息
            $scope.clicked = [];
            $scope.stepValid = [];

            $scope.demandFields = MetaConstants.demandFields;
            $scope.formData = {
                title: undefined,
                areas: [],
                cooperationWays: [],
                researchType: undefined,
                researchPeriod: [],
                researchFunding: [],
                earnestMoney: undefined,
                details: undefined,
                indicators: undefined,
                theUninteresting: undefined,
                attachments: [],
                contactInfo: {
                    name: $rootScope.USER.name,
                    phone: $rootScope.USER.mobile,
                },
                relationWithOwner: undefined,
                willingToFollow: undefined,
                enterpriseInfo: {
                    name: undefined,
                    scale: undefined,
                    province: undefined,
                    city: undefined,
                    district: undefined,
                    contactName: undefined,
                    contactPhone: undefined
                },
                state: '新提交'
            };

            /**
             * provinces
             * cities
             * @type {Array}
             */
            $scope.provinces = MetaConstants.provinces;
            $scope.cities = MetaConstants.cities;

            /**
             * step2 editor config
             */


            $scope.data = {
                availableOptions: [
                    {id: '1', name: 'Option A'},
                    {id: '2', name: 'Option B'},
                    {id: '3', name: 'Option C'}
                ],
                selectedOption: {id: '3', name: 'Option C'} //This sets the default value of the select in the ui
            };

            $scope.verifyModel = function () {
                $rootScope.openOfficerVerifyModal().result.then(function () {
                    //$scope.formData.willingToFollow = 'true';
                }, function () {
                    //$scope.formData.willingToFollow = undefined;
                });
            };

            //步骤一下一步
            $scope.stepOneNext = function (valid) {
                $scope.clicked[0] = true;
                if (valid && $scope.formData.areas.length !== 0 && $scope.formData.cooperationWays.length !== 0) {
                    $scope.stepValid[0] = true;
                    $state.go('^.two');
                } else {
                    MetaRemind.error('请按要求完成步骤一的填写');
                }
            };

            //步骤二下一步
            $scope.stepTwoNext = function (valid) {
                $scope.clicked[1] = true;
                if (valid) {
                    $scope.stepValid[1] = true;
                    $state.go('^.three');
                } else {
                    MetaRemind.error('请按要求完成步骤二的填写');
                }
            };

            //步骤三下一步
            $scope.stepThreeNext = function (valid) {
                //点击则显示为true
                $scope.clicked[2] = true;
                if (valid) {
                    $scope.stepValid[2] = true;
                    $scope.submit();
                } else {
                    MetaRemind.error('请按要求完成步骤三的填写');
                }
            };

            //提交需求
            $scope.submit = function () {

                var states = ['one', 'two', 'three'];
                var remind = ['一', '二', '三'];

                for (var i = 0; i < $scope.stepValid.length; ++i) {
                    if (!$scope.stepValid[i]) {
                        $state.go('^.' + states[i]);
                        MetaRemind.info('请完成步骤' + remind[i] + '的填写!');
                        return;
                    }
                }

                MetaResources.demand.create({userId: '_self'}, $scope.formData, function (data) {
                    MetaRemind.success('上传成功!');
                    $state.go('^.success', {
                        toState: 'meta.demand.list.released',
                        moduleName: 'demand',
                        moduleId: data.id
                    });
                }, function (error) {
                    MetaRemind.error(error);
                });

            };

        }]);
